package graduate.wangyaqi.Account;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.transaction.Transactional;


@Service
public class UserService {
    @Resource
    private UserRepository userRepository;
    /**
     * save,update,delete方法需要绑定事务。
     * 使用@Transactional进行事务绑定。
     *
     */
    // 保存数据
    @Transactional
    public void save(User user) {
        userRepository.save(user);
    }

    // 删除数据
    @Transactional
    public void delete(long id) {
        userRepository.deleteById(id);
    }

    @Transactional
    public void deleteAll(){
        userRepository.deleteAll();
    }

    // 查询数据
    public Iterable<User> getAll() {
        Iterable<User> users=userRepository.findAll();
        return users;
    }

    // 验证邮箱
    public User findByEmail(String email) {
        User user=userRepository.findByEmail(email);
        return user;
    }

    public User findUserByEmailAndPassword(String email, String password){
        return userRepository.findUserByEmailAndPassword(email,password);
    }

    public User findUserByEmailAndRandNum(String email,String randNum){
        return userRepository.findUserByEmailAndRandNum(email, randNum);
    }
}
